;
(function() {
	var skill = {
		getAssshopList: function() {
			var assshop = this.config.assshop || [];
			assshop = $Array.clearNull(assshop.map(function(bus_info) {
				if (bus_info.bus_id) {
					var bus = BusCon.get(bus_info.bus_id);
					if (bus) {
						bus.url = bus.getOneVisitableUrl();
						return {
							bus: bus
						};
					}
				}
			}));
			return assshop;
		},
		getRequireAssshopList: function() {
			return BusAssshopCon.getByMyBusId(this._id).map(function(assshop) {
				assshop = $Object.clone(assshop);
				assshop.acc_bus = BusCon.get(assshop.acc_bus_id);
				return assshop;
			});
		},
		//添加联合店铺
		addAssshop: function(vcode) {
			var assshop = BusAssshopCon.getByVcode(vcode);
			if (!assshop) {
				throwCusError("申请码无效")
			} else if (assshop.acc_bus_id !== this._id) {
				throwCusError("无权使用此申请码")
			}
			(this.config.assshop || (this.config.assshop = [])).unshift({
				bus_id: assshop.my_bus_id
			});
			// this.checkConfig();
			//申请码使用后，自动销毁
			assshop.destroy();
		},
		//解除联合
		removeAssshop: function(bus_id) {
			this.config.assshop = (this.config.assshop || []).filter(function(bus_info) {
				return bus_info.bus_id !== bus_id
			});
			this.save();
		},
		//请求联合申请码
		requireAssshop: function(phone) {
			if (!helper.phone(phone)) {
				throwCusError("手机号码格式错误")
			}
			var bus = db.find_one(config.dbname.bus, {
				info: {
					mobile_phone: phone
				}
			});
			if (!bus) {
				throwCusError("找不到指定商家")
			} else {
				bus = BusCon.getInstance(bus)
			}
			if ((this.config.assshop || []).filter(function(bus_info) {
					return bus_info.bus_id == bus._id
				}).length) {
				throwCusError("已经是关联店铺，不可重复关联");
			}
			bus._buildAssshopVcode(this._id);
		},
		//非安全，生成联合申请码
		_buildAssshopVcode: function(bus_id) {
			var assshop = BusAssshopCon.build(this._id, bus_id)
			return assshop;
		}
	};
	module.exports = skill;
	var db = require("../db/index");
	var DomainCon = require("../domain/_con");
	var GoodsCon = require("../goods/_con");
	var UserCon = require("../../core/user/_con");
	var BusCon = require("../../core/bus/_con");
	var AchCon = require("../ach/_con");
	var ClassifyCon = require("../bus.classify/_con");
	var TagCon = require("../bus.tag/_con");
	var LeagueCon = require("../league/_con");
	var BuffCon = require("../buff/_con");
	var BusUserBindCon = require("../bus_user_bind/_con");
	var BusGuideCon = require("../bus.guide/_con");
	var BusAssshopCon = require("../bus.assshop/_con");
	var CommentCon = require("../comment/_con");
	var config = require("../_include/config");
	var data_cut = require("../_include/data_cut");
	var helper = require("../_include/helper");
	var md5 = require("../_include/md5");
	var $Object = require("../_include/$.Object");
	var $Array = require("../_include/$.Array");
	var tld = require('tldjs');
	var fs = require("fs");
	var Fiber = require('fibers');
}());